LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;

--RAM
--16位修改为8位

ENTITY asynram IS
GENERIC(ram_width: POSITIVE :=8;
        adr_width: POSITIVE :=8); 
PORT (  din  : IN  STD_LOGIC_VECTOR((ram_width-1) DOWNTO 0);
		dout : OUT STD_LOGIC_VECTOR((ram_width-1) DOWNTO 0);
		adr  : IN  STD_LOGIC_VECTOR((adr_width-1) DOWNTO 0);
		wr   : IN  STD_LOGIC		
);
END asynram;

ARCHITECTURE rtl OF asynram IS
SUBTYPE ram_word IS STD_LOGIC_VECTOR(0 TO (ram_width-1) );
TYPE ram_type IS ARRAY (0 TO 255) OF ram_word;                   --ram的最大存储长度由32改为256
SIGNAL ram:ram_type;

BEGIN
	PROCESS(wr) 	BEGIN
													   --前四个节拍  				   	-> R0=00000000
		ram(conv_integer("00000000")) <= "11100000";   --MVRD R0,00000100     	-> R0=00000100 
		ram(conv_integer("00000001")) <= "00000100";
		
		ram(conv_integer("00000010")) <= "11000100";   --MVRR R1,R0 			-> R0=00000100  R1=00000100  
		
		ram(conv_integer("00000011")) <= "00000001";   --ADD R0,R1  			-> R0=00001000
		
		ram(conv_integer("00000100")) <= "00010000";   --SUB R0,R1  			-> R0=00000000
		
		ram(conv_integer("00000101")) <= "10110000";   --STC					-> R0=00000000  C=1  
		
		ram(conv_integer("00000110")) <= "01100001";   --ADC R0,R1				-> R0=00000101
		
		ram(conv_integer("00000111")) <= "11101000";   --MVRD R2,11111111    	-> R0=00000101  R2=11111111
		ram(conv_integer("00001000")) <= "11111111"; 
		
		ram(conv_integer("00001001")) <= "00110010";   --OR R0,R2				-> R0=11111111
		
		ram(conv_integer("00001010")) <= "11101100";   --MVRD R3,00000001     	-> R0=11111111  R3=00000001
		ram(conv_integer("00001011")) <= "00000001"; 
		
		ram(conv_integer("00001100")) <= "00100011";   --AND R0,R3 				-> R0=00000001
		
		ram(conv_integer("00001101")) <= "01010011";   --CMP R0,R3    			-> R0=00000001  Z=1
		
		ram(conv_integer("00001110")) <= "10100001";   --JRZ PC+1				-> R0=00000001
		
		ram(conv_integer("00001111")) <= "01000000";   --XOR R0,R0   			-> R0=00000000  正确结果应是跳过此条
		
		ram(conv_integer("00010000")) <= "11110001";   --LDRR R0,[R1]   		-> R0=00010001		
		
		ram(conv_integer("00010001")) <= "10000000";   --INC R0  				-> R0=00010010
		
		ram(conv_integer("00010010")) <= "11010000";   --JMPA ADR				-> R0=00010010
		ram(conv_integer("00010011")) <= "00010101";
		
		ram(conv_integer("00010100")) <= "01000000";   --XOR R0,R0   			-> R0=00000000  正确结果应是跳过此条
		
		ram(conv_integer("00010101")) <= "10010000";   --SHL R0   				-> R0=00100100
		
		ram(conv_integer("00010110")) <= "01000000";   --XOR R0,R0   			-> R0=00000000
		
		IF wr='1' THEN
		ram(conv_integer(adr)) <= din;
		dout <= (others => 'Z');
        ELSE
		dout <= ram(conv_integer(adr));
		END IF;
	END PROCESS;
END rtl;